# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
import requests
from bs4 import BeautifulSoup
import pandas as pd

def fetch_data(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # 如果请求失败，抛出HTTPError异常
        return response.text
    except requests.exceptions.RequestException as e:
        print("请求网页数据失败:", e)
        return None

def parse_data(html):
    soup = BeautifulSoup(html, 'html.parser')
    # 根据实际情况解析数据
    data_list = []
    for item in soup.find_all('div', class_='movie-item-info'):
        title = item.find('p', class_='name').text.strip()
        star = item.find('p', class_='star').text.strip()
        release_time = item.find('p', class_='releasetime').text.strip()
        data_list.append([title, star, release_time])
    return data_list

def save_to_excel(data, sheet_name, excel_writer):
    df = pd.DataFrame(data, columns=['电影名称', '主演', '上映时间'])
    df.to_excel(excel_writer, sheet_name=sheet_name, index=False)

def main():
    # 从urls.txt文件中读取网址列表
    with open('urls.txt', 'r') as f:
        urls = f.readlines()
    urls = [url.strip() for url in urls]

    with pd.ExcelWriter('data.xlsx') as writer:
        for idx, url in enumerate(urls):
            print(f"正在处理网址 {idx+1}/{len(urls)}: {url}")
            html = fetch_data(url)
            if html:
                data = parse_data(html)
                sheet_name = f'Sheet{idx+1}'
                save_to_excel(data, sheet_name, writer)
                print(f"已保存到 Excel 文件中的 {sheet_name} 工作表")
            else:
                print(f"无法获取网页数据：{url}")

if __name__ == "__main__":
    main()
